package org.oep;

public class Utils {
	public static String preciseTimeDiff(long diff) {
		int ms = (int) diff % 1000;
		int s = (int) diff / 1000;
		return s + "." + ms + "s";
	}
	
	public static String friendlyTimeDiff(long diff) {
		int ms = (int) diff % 1000;
		int seconds = (int) diff / 1000;
		int minutes = seconds / 60;
		int hours = minutes / 60;
		int days = hours / 24;
		int weeks = days / 7;
		
		/*
		System.out.println("Difference: " + diff);
		System.out.println("Seconds: " + seconds);
		System.out.println("Minutes: " + minutes);
		System.out.println("Hours: " + hours);
		System.out.println("Days: " + days);
		System.out.println("Weeks: " + weeks);
		*/
		
		String out = new String();

		if(weeks > 0){
			out += weeks + "w";
		} else if(days > 0) {
			out += days + "d";
		} else if(hours > 0) {
			out += hours + "h";
		} else if(minutes > 0) {
			out += minutes + "m";
		} else if(seconds > 0){
			out += seconds + "s";
		} else {
			out += ms + "ms";
		}
		return out;
	}
	
	public static String friendlyTimeDiffNow(long time) {
		return friendlyTimeDiff(System.currentTimeMillis() - time);
	}
	
	public static String [] tokenize(String s) {
		int spaceCount = 0;
		for(int i = 0; i < s.length(); i++)
			if(s.charAt(i) == ' ') spaceCount++;
		String [] tokens = new String [spaceCount + 1];
		String tmp = new String();
		int index = 0;
		for(int i = 0; i < s.length(); i++) {
			if(s.charAt(i) != ' ')
				tmp += s.charAt(i);
			else {
				tokens[index] = tmp;
				tmp = "";
				index++;
			}
		}
		tokens[ tokens.length - 1 ] = tmp;
		return tokens;
	}
	
	/* Tests friendlytimediff
	public static void main(String [] args) {
		for(long i = 0; i < (24 * 60 * 60 * 1000); i+=1000) {
			System.out.println(i + " - " + friendlyTimeDiff(i) );
		}
	}*/
}
